package show.me.the.money.validator;

import java.math.BigDecimal;
import java.util.HashMap;
import org.apache.commons.lang.StringUtils;

public class GenericValidator {
	
	private static HashMap<String, Integer> types = new HashMap<String,Integer>();
	static{
		types.put("bigDecimal",1);
		types.put("int",2);
		types.put("long",3);
		types.put("short",4);
		types.put("float",5);
		types.put("double",6);
	}
	
    public GenericValidator(){
    }

    public static boolean isNotNull(String value){
        return !StringUtils.isEmpty(value);
    }

    public static boolean isRegexp(String value, String regexp){
        if(regexp == null || regexp.length() <= 0){
            return false;
        }else{
            return value.matches(regexp);
        }
    }

    public static boolean isEmail(String value){
    	String regex = "^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$"; 
    	if(StringUtils.isEmpty(value)){
    		return false;
    	}else{
    		return value.matches(regex);
    	}
    }
    
    public static boolean isMobile(String value){
    	String regex = "^1[358][0-9]\\d{8}$"; 
    	if(StringUtils.isEmpty(value)){
    		return false;
    	}else{
    		return value.matches(regex);
    	}   	
    }
    
    public static boolean isPostCode(String value){
    	String regex = "^[1-9]\\d{5}$"; 
    	if(StringUtils.isEmpty(value)){
    		return false;
    	}else{
    		return value.matches(regex);
    	} 
    }

    public static boolean maxLength(String value, String max){
    	int n = 0;
		try {
			n = Integer.parseInt(max);
		} catch (NumberFormatException e) {
			return false;
		}
        return value.length() <= n;
    }
    
    public static boolean maxLength(String value, int max){
    	return value.length() <= max;
    }

    public static boolean minLength(String value, String min){
    	int n = 0;
    	try {
			n = Integer.parseInt(min);
		} catch (NumberFormatException e) {
			return false;
		}
        return value.length() >= n;
    }
    
    public static boolean minLength(String value, int min){
    	return value.length() >= min;
    }

    public static boolean maxValue(String value, String max, String type){
    	Integer tp = types.get(type);
    	if(tp == null){
    		return true;
    	}
    	switch(tp){
		  case 1: 
			  try{
				  BigDecimal v = BigDecimal.valueOf(Double.parseDouble(value));
				  BigDecimal v_max = BigDecimal.valueOf(Double.parseDouble(max));
				  return v.compareTo(v_max) <= 0;
			  }catch(Exception e){
				  return false;
			  }
		  case 2: 
			  return Integer.valueOf(value) <= Integer.valueOf(max);
		  case 3:
			  return Long.valueOf(value) <= Long.valueOf(max);
		  case 4:
			  return Short.valueOf(value) <= Short.valueOf(max);
		  case 5:
			  return Float.valueOf(value) <= Float.valueOf(max);
		  case 6:
			  return Double.valueOf(value) <= Double.valueOf(value);
			  
    	}
    	return false;
    }
    
    public static boolean minValue(String value, String max, String type){
    	Integer tp = types.get(type);
    	if(tp == null){
    		return true;
    	}
    	switch(tp){
		  case 1: 
			  try{
				  BigDecimal v = BigDecimal.valueOf(Double.parseDouble(value));
				  BigDecimal v_max = BigDecimal.valueOf(Double.parseDouble(max));
				  return v.compareTo(v_max) >= 0;
			  }catch(Exception e){
				  return false;
			  }
		  case 2: 
			  return Integer.valueOf(value) >= Integer.valueOf(max);
		  case 3:
			  return Long.valueOf(value) >= Long.valueOf(max);
		  case 4:
			  return Short.valueOf(value) >= Short.valueOf(max);
		  case 5:
			  return Float.valueOf(value) >= Float.valueOf(max);
		  case 6:
			  return Double.valueOf(value) >= Double.valueOf(value);
			  
    	}
    	return false;
    }
       
}
